<template>
  <div class="home">    
    <el-container>
      <el-header>
        <el-menu
            default-active="1"
            class="el-menu-demo"
            mode="horizontal"
            :class="switchbtn? 'moon' : ''"
            :ellipsis="false"
            @select="handleSelect"
          >
          <el-menu-item index="0">
            <img
              style="width: 60px"
              src="https://free-img.400040.xyz/4/2024/07/03/6684e4ee7f3c0.png"
              alt="logo"
            />
          </el-menu-item>
          <div class="flex-grow"> Aclfy's Blog </div>
          <div class="switch">
            <el-switch v-model="switchbtn">
              <template #inactive-action>
                <span class="custom-active-action">
                  <el-icon><i-ep-Sunny /></el-icon>
                </span>
              </template>
              <template #active-action>
                <span class="custom-inactive-action">
                  <el-icon><i-ep-Moon /></el-icon>
                </span>
              </template>
            </el-switch>
          </div>
          <el-sub-menu index="2">
            <template #title>
            个人中心 
            <img :src="userinfo?.avatar || `https://free-img.400040.xyz/4/2024/06/24/66794c3bcb54b.jpg`" style="width: 30px;border-radius: 50%; margin-left: 5px;" alt="">
            </template>
            <el-menu-item index="2-1">修改密码</el-menu-item>
            <el-menu-item index="2-2">退出登录</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-header>
      <div class="header">
        <div class="left">
          <div class="logo">
            <img :src="userinfo?.avatar || `https://free-img.400040.xyz/4/2024/06/24/66794c3bcb54b.jpg`" style="width: 150px;border-radius: 25px; padding: 10px" alt="">
          </div>
          <div class="userinfo">
            <div class="name">爱吃罗非鱼</div>
            <div class="label"><img src="../assets/SVIP.svg" alt="" style="width: 80px;"><span style="color:#FCC12D;position: relative;left: 5px;">超级管理员</span> </div>
            <div class="desc">{{userinfo?.desc}}</div>
          </div>
        </div>
        <div class="center">
        </div>
        <div class="right">
          <el-button  type="primary" @click="toedit" ><el-icon><i-ep-Edit /></el-icon>编辑</el-button>
        </div>
      </div>
      <el-container>
        <el-menu
            :default-active="activmenu"
            class="el-menu-vertical-demo"
            :collapse="isCollapse"
            :class="switchbtn? 'moon' : ''"
            @select="handleSelect">
          <el-radio-group v-model="isCollapse" style="margin: 10px auto;display: flex;justify-content: center;width: 100%;">
            <el-radio-button v-if="isCollapse" :value="false">展开</el-radio-button>
            <el-radio-button v-if="!isCollapse" :value="true">折叠</el-radio-button>
          </el-radio-group>
            <el-sub-menu index="3">
              <template #title>
                <el-icon><i-ep-Edit /></el-icon>
                <span>文章</span>
              </template>
              <el-menu-item-group>
                <el-menu-item index="3-1">编写文章</el-menu-item>
                <el-menu-item index="3-2">文章管理</el-menu-item>
              </el-menu-item-group>
            </el-sub-menu>
            <el-sub-menu index="4">
              <template #title>
                <el-icon><i-ep-User /></el-icon>
                <span>个人信息</span>
              </template>
                <el-menu-item-group>
                <el-menu-item index="4-1">修改密码</el-menu-item>
                <el-menu-item index="4-2">留白备用</el-menu-item>
              </el-menu-item-group>
            </el-sub-menu>
            <el-sub-menu index="5">
              <template #title>
                <el-icon><i-ep-setting /></el-icon>
                <span>设置</span>
              </template>
                <el-menu-item-group>
                <el-menu-item index="5-1">爱好管理</el-menu-item>
                <el-menu-item index="5-2">轮播图管理</el-menu-item>
              </el-menu-item-group>
            </el-sub-menu>
          </el-menu>
        <el-main 
        style="padding: 20px;"
        :class="switchbtn? 'moon' : ''">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { login } from '../apis/user'
onMounted(async()=>{
  activmenu.value = sessionStorage.getItem('activmenu')
  getuserinfo()
})
const toedit = ()=>{
  console.log('去编辑');
  router.push('/user')
}
const router = useRouter()
const activmenu = ref('4-1')
const switchbtn = ref(false)
const isCollapse = ref(false)
const handleSelect = (val)=>{
  activmenu.value = val
  sessionStorage.setItem('activmenu',activmenu.value)
  console.log(val, 'val')
  switch (val) {
    case '2-1': //修改密码
      router.push('/user')
      break;
    case '2-2': //退出登录
      //清除cookie跳转登录页  已退出当前账号！
      localStorage.removeItem('cms_cookie')
      router.push('/login')
      break;
    case '3-1': // 文章编写
      router.push('/createartic')
      break;
        case '3-2': //文章管理
      router.push('/articlist')
      break;
    case '4-1'://个人信息
      router.push('/user')
      break;
    case '4-2': //liubai
      router.push('/home')
      break;
    case '5-1'://爱好管理
      router.push('/likesetting')
      break;
    case '5-2'://轮播图管理
      router.push('/bannersetting')
      break;
  }
}
let userinfo = ref(null)
const getuserinfo = () => {
  login().then(res=>{
    if (res.data.errmsg) {
      ElMessage({
        message: 'Token过期！',
        type: 'error',
      })
      localStorage.removeItem('cms_cookie')
      router.push('/login')
    } else {
      localStorage.setItem('cms_cookie', res.data.token)
      userinfo.value = res.data.data[0]
      console.log(userinfo.value, 'values');
      localStorage.setItem('cms_userinfo', JSON.stringify(userinfo.value))
    }
  })
}


</script>

<style lang="less" scoped>
@sun: #f5f5ff;
@moon: #aaa;
.home > .el-container{
  background-color: @sun;
  width: 100%;
  height: 100vh;
}
.moon{
  background-color: @moon !important;
}
.header{
  display: flex;
  align-items: center;
  padding: 20px 50px;
  margin: 20px;
  border-radius: 25px;
  background-color: #fff;
  .left{
    flex: 1;
    display: flex;
    .userinfo{
      padding: 10px;
      max-width: 200px;
      .name{
        padding-bottom: 25px;
        font-size: 24px;
        font-weight: 700;
        color: #0ff;
      }
      .label{
        position: relative;
        right: 10px;
        font-size: 18px;
        font-weight: 700;
      }
      .desc{
        padding-top: 20px;
        color: #aaa;
      }
    }
  }
  .center{
    flex: 1;
  }
}
img{
  object-fit: cover;
}
.flex-grow{
  flex-grow: 1;
  text-align: center;
  margin: auto 0;
  color: #0ff;
  text-shadow: -1px 0 0 #0ff;
  font-size: 24px;
}
.switch{
  text-align: center;
  margin: auto 0;
}
.el-header{
  padding: 0;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>
